﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Security.Permissions;

namespace MikeKappel.Com.CS.CustomControls
{
    [Bindable(true)]
    /// <summary>
    /// Summary description for Button
    /// </summary>
    public class MikeRadioButton : RadioButton
    {
        [Bindable(true)]
        [Category("Data")]
        [Localizable(true)]
        [DefaultValue("")]
        public String Value
        {
            get
            {
                return (String)ViewState["Value"];
            }
            set
            {
                ViewState["Value"] = value;
            }
        }

        public String PostedValue
        {
            get
            {
                return (String.IsNullOrEmpty(GroupName) || HttpContext.Current.Request.Form[GroupName] == null) ? null : HttpContext.Current.Request.Form[GroupName];
            }
        }

        public override bool Checked
        {
            get
            {
                if (!isChecked.HasValue && PostedValue != null && PostedValue == Value)
                    isChecked = true;

                return (isChecked.HasValue) ? isChecked.Value : false;
            }
            set
            {
                isChecked = value;
            }
        }
        private Boolean? isChecked;


        /// <summary>
        /// Adds to load event
        /// </summary>
        public String OnClientLoad
        {
            get
            {
                return onClientLoad;
            }
            set
            {
                onClientLoad = value;
            }
        }
        private String onClientLoad;

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!String.IsNullOrEmpty(onClientLoad))
                MikeKappel.Com.CS.Javascript.AddOnLoad(this.Page, onClientLoad.Replace("this.", "document.getElementById('" + this.ClientID + "')."));
        }
        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            base.Render(hw);
            String fixedHTML = MikeKappel.Com.CS.Helper.HtmlTagAttributeReplace(hw.InnerWriter.ToString(), "input", "name", GroupName);
            fixedHTML = MikeKappel.Com.CS.Helper.HtmlTagAttributeReplace(fixedHTML, "input", "value", Value);
            fixedHTML = MikeKappel.Com.CS.Helper.HtmlTagAttributeRemove(fixedHTML, "input", "checked");
            if (Checked)
                fixedHTML = MikeKappel.Com.CS.Helper.HtmlTagAttributeAdd(fixedHTML, "input", "checked", "checked");
            sb = new StringBuilder(fixedHTML);
            writer.Write(sb);
        }
    }
}
